package com.zhijiaoyun.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zhijiaoyun.common.StringUtils;
import com.zhijiaoyun.common.ResponseUtils;
import com.zhijiaoyun.dao.ICourseRecommendDao;
import com.zhijiaoyun.model.CourseRecommend;
import com.zhijiaoyun.model.dto.CourseDto;
import com.zhijiaoyun.model.dto.CourseRecommendDto;
import com.zhijiaoyun.model.dto.PageData;
import com.zhijiaoyun.model.dto.PageList;
import com.zhijiaoyun.service.ICourseRecommendService;
import com.zhijiaoyun.service.ICourseService;

/**
 * @author shisfish
 * @date 2017/6/6
 */
@Service(value = "courseRecommendService")
public class CourseRecommendServiceImpl implements ICourseRecommendService {

	@Resource
	ICourseRecommendDao courseRecommendDao;
	
	@Resource
	ICourseService courseService;

	@Override
	public CourseRecommend getCourseRecommendByCourseId(Long courseId) {
		return courseRecommendDao.selectByCourseId(courseId);
	}

	@Override
	public void addCourseRecommendService(CourseRecommend courseRecommend) {
		courseRecommendDao.insert(courseRecommend);
	}

	@Override
	public void deleteCourseRecommendService(CourseRecommend courseRecommend) {
		courseRecommendDao.deleteByPrimaryKey(courseRecommend.getRecommendId());
	}

	@Override
	public List<CourseRecommendDto> getCourseRecommentByParamsDto() {
		List<CourseRecommendDto> courseRecommendDtos = new ArrayList<>();
		List<CourseRecommend> courseRecommends = courseRecommendDao.selectAll();
		if (!StringUtils.isEmptyList(courseRecommends)) {
			for (CourseRecommend courseRecommend : courseRecommends) {
				CourseDto courseDto = courseService.getCourseDtoByCourseId(courseRecommend.getCourseId());
				if (courseDto != null) {
					CourseRecommendDto courseRecommendDto = new CourseRecommendDto(courseRecommend, courseDto);
					courseRecommendDtos.add(courseRecommendDto);
				}
			}
		}
		return courseRecommendDtos;
	}

	@Override
	public PageList<CourseRecommendDto> pageCourseRecommentByParamsDto(int pageIndex, int pageSize) {
		int count = courseRecommendDao.countByParams();
		PageList<CourseRecommendDto> courseRecommendPage = new PageList<>();
		if (count > 0) {
			PageData page = PageData.createPage(count, pageIndex, pageSize);
			List<CourseRecommendDto> courseRecommendDtos = new ArrayList<>();
			List<CourseRecommend> courseRecommends = courseRecommendDao.pageByParams(page.getBeginIndex(), page.getPageSize());
			if (!StringUtils.isEmptyList(courseRecommends)) {
				for (CourseRecommend courseRecommend : courseRecommends) {
					CourseDto courseDto = courseService.getCourseDtoByCourseId(courseRecommend.getCourseId());
					if (courseDto != null) {
						CourseRecommendDto courseRecommendDto = new CourseRecommendDto(courseRecommend, courseDto);
						courseRecommendDtos.add(courseRecommendDto);
					}
				}
			}
			courseRecommendPage.setList(courseRecommendDtos);
			courseRecommendPage.setPageData(page);
		}
		
		return courseRecommendPage;
	}

	@Override
	public Map<String, Object> page(int pageIndex, int pageSize) {
		PageList<CourseRecommendDto> courses = pageCourseRecommentByParamsDto(pageIndex, pageSize);
		Map<String, Object> map = new HashMap<>();
		map.put("list", courses.getList());
		map.put("page", courses.getPageData());
		return ResponseUtils.responseSuccessMap(map);
	}

	@Override
	public Map<String, Object> updateRecommend(Long courseId, String createUser) {
		CourseRecommend courseRecommend = getCourseRecommendByCourseId(courseId);
		if (courseRecommend == null) {
			courseRecommend = new CourseRecommend(courseId, "", null, createUser);
			addCourseRecommendService(courseRecommend);
		} else {
			deleteCourseRecommendService(courseRecommend);
		}
		return ResponseUtils.responseSuccessMap();
	}


}
